package com.zyx.javademo.exception;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/28 17:57
 * desc: 线程池中捕获的异常交由线程本身处理
 * reference: https://honeypps.com/java/java-multi-thread-of-uncaught-exception-handler/
 * {@link ExecuteCaught} 中线程池使用UncaughtExceptionHandler捕获异常失败
 * 需要将异常的捕获封装到Runnable或者Callable中
 */
public class ExecuteCaughtRunnable {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(new ThreadPoolTask());
        exec.shutdown();
    }
}

class ThreadPoolTask implements Runnable {
    @Override
    public void run() {
        Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());
        System.out.println(3 / 2);
        System.out.println(3 / 0);
        System.out.println(3 / 1);
    }
}